package org.example.ymcc.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.example.ymcc.dto.CourseAddDto;
import org.example.ymcc.service.ICourseService;
import org.example.ymcc.domain.Course;
import org.example.ymcc.query.CourseQuery;
import org.example.ymcc.result.JsonResult;
import org.example.ymcc.result.PageList;
import org.example.ymcc.util.jwt.RsaUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;

    @PostMapping("/info")
    public JsonResult info(@RequestBody List<Long> courseIds){
        return courseService.info(courseIds);
    }


    @GetMapping("/detail/data/{courseId}")
    public JsonResult getDetailData(@PathVariable("courseId")Long courseId){
        return courseService.getDetailData(courseId);
    }

    @PostMapping("/onLineCourse")
    public JsonResult onLineCourse(@RequestBody List<Long> ids){
        return courseService.onLineCourse(ids);
    }

    @GetMapping("/detail/{courseId}")
    public JsonResult getDetail(@PathVariable("courseId")Long courseId){
        return courseService.getDetail(courseId);
    }

    /**
    * 保存和修改公用的
    */
    @PostMapping("/save")
    public JsonResult saveOrUpdate(@RequestBody  @Valid CourseAddDto course, HttpServletRequest request){
        String token = request.getHeader("token");
        JSONObject currentLogin = RsaUtils.getCurrentLogin(token);

        if(course.getCourse().getId()!=null){
            courseService.updateById(course);
        }else{
            courseService.save(course,currentLogin);
        }
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        courseService.removeById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @GetMapping("/{id}")
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success().setData(courseService.getById(id));
    }


    /**
    * 查询所有对象
    */
    @GetMapping("/list")
    public JsonResult list(){
        return JsonResult.success().setData(courseService.list(null));
    }


    /**
    * 带条件分页查询数据
    */
    @PostMapping("/pagelist")
    public JsonResult page(@RequestBody CourseQuery query){
        Page<Course> page = new Page<Course>(query.getPageNo(), query.getPageSize());
        LambdaQueryWrapper<Course> wrapper = new LambdaQueryWrapper<Course>()
                .like(StringUtils.isNotEmpty(query.getKeyword()), Course::getName, query.getKeyword());
        page = courseService.page(page,wrapper);
        return JsonResult.success().setData(new PageList<Course>(page.getTotal(), page.getRecords()));
    }
}
